package xupt.com.bolg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xupt.com.bolg.entity.Bolg;
import xupt.com.bolg.repository.BolgRepository;
import xupt.com.bolg.service.BolgService;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class BolgServiceImpl implements BolgService {
    @Autowired
    BolgRepository bolgRepository;
    @Override
    public boolean save(Bolg bolg) {

        bolg.setData(new Date());
        bolgRepository.save(bolg);
        return true;
    }

    @Override
    public boolean deleteByID(Integer id) {
        bolgRepository.deleteById(id);
        return true;
    }

    @Override
    public Bolg findByID(Integer id) {
        Optional<Bolg> byId = bolgRepository.findById(id);
        return byId.get();
    }

    @Override
    public List<Bolg> findAll() {
        List<Bolg> all = bolgRepository.findAll();
        return all;
    }
    @Override
    public boolean updateBolg(Bolg bolg) {
        bolg.setUp(bolg.getUp());
        bolgRepository.save(bolg);
        return true;
    }

    @Override
    public boolean lowBolg(Bolg bolg) {
        bolgRepository.save(bolg);
        return true;
    }

    @Override
    public boolean deleteByUserName(String name) {
        List<Bolg> all = bolgRepository.findAll();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getUserName().equals(name)){
                bolgRepository.deleteById(all.get(i).getId());
            }
        }
        return false;
    }
}
